/*
 *  PKL 51 GUNUNG KIDUL
 *  Subseksie DATA ENTRY + CAPI
 */
package pkl51;

import java.awt.*;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import pkl51.component.Panel;
import pkl51.database.DatabaseUtility;
import pkl51.database.DbConfigUtility;
import pkl51.view.FormUtama;
import usu.widget.Label;

/**
 *
 * @author DE+CAPI PKL51 = Hanif, Hindarwan, Eri, Fitri, Tyas, Thio, Herlambang,
 * Sason
 */
public class Main extends JWindow {

    private int durasi;
    private JProgressBar progressBar;

    public Main(int d) {
        durasi = d;
    }

    public void showSplash() {
        Panel panel = new Panel();
        progressBar = new JProgressBar();
        progressBar.setPreferredSize(new Dimension(300, 20));
        progressBar.setForeground(new Color(0,153,0));
        progressBar.setStringPainted(true);
        progressBar.setIndeterminate(true);
        progressBar.setString("Connecting to server... ");

        int width = 480;
        int height = 320;
        Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
        int x = (screen.width - width) / 2;
        int y = (screen.height - height) / 2;
        setBounds(x, y, width, height);

        Label author = new Label();
        author.setText("CREATED BY DE-CAPI PKL51");
        author.setFont(new Font("Comicsans", 1, 12));
        author.setForeground(Color.GRAY);
        panel.add(author, BorderLayout.NORTH);
        Label logo = new Label();
//        logo.setIconReflec(new javax.swing.ImageIcon(getClass().getResource("splash.png")));
        logo.setIcon(new javax.swing.ImageIcon(getClass().getResource("image/splash.png")));
        panel.add(logo, BorderLayout.CENTER);
        panel.add(progressBar, BorderLayout.SOUTH);
        add(panel);

        setVisible(true);

        try {
            Thread.sleep(durasi);
            Thread connectionThread = new Thread(new StartConnection());
            connectionThread.start();
            connectionThread.join(); //tunggu sampai koneksi berhasil dibuat
        } catch (InterruptedException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }
//        setVisible(false);
    }

    public void showSplashAndExit() {
        showSplash();
        System.exit(0);
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        try {
            UIManager.setLookAndFeel(new com.sun.java.swing.plaf.windows.WindowsLookAndFeel());
        } catch (UnsupportedLookAndFeelException ex) {
            System.out.println("Use default look and feel");
        }
//        start server, jika mau pake server mysql portabel, uncomment code dibawah ini
//        Thread threadStartServer = new Thread(new StartServer());
//        threadStartServer.start();

        final Main splash = new Main(3000);
        splash.showSplash();
        
        javax.swing.SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                FormUtama.getInstance().setVisible(true);
                splash.dispose();
            }
        });
    }

    private class StartConnection implements Runnable {

        @Override
        public void run() {
            try {
                DatabaseUtility.getConnection();
                progressBar.setIndeterminate(false);
                progressBar.setValue(25);
                Thread.sleep(500);
                progressBar.setValue(50);
                Thread.sleep(500);
                progressBar.setValue(100);
                progressBar.setString("Connected to " + DbConfigUtility.getServerName());
            } catch (InterruptedException ex) {
                Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
            } catch (SQLException ex) {
                progressBar.setString("Connection failed!");
                progressBar.setIndeterminate(false);
//                progressBar.setValue(100);
                JOptionPane.showMessageDialog(null, "Koneksi ke database gagal", "Error", JOptionPane.ERROR_MESSAGE);
                System.exit(1);
                Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}
